package com.twu.biblioteca.ui;

import com.twu.biblioteca.Persistence.UserRepository;
import com.twu.biblioteca.library.Library;
import com.twu.biblioteca.library.UserLoginController;
import com.twu.biblioteca.library.items.User;

import java.util.Scanner;

/**
 * Created by aruna on 29/4/14.
 */
public class LoggedInUserMenu {
    private User user;
    //For user story 2
    public LoggedInUserMenu(User user)
    {
        this.user=user;
    }
    public LoggedInUserMenu()
    {
        this.user= UserRepository.getUserByLibraryNumber("111-0000");
    }

    public void displayListofBooks()
    {
        System.out.println(Library.getBookReservationManager().getBooksRepository().getBookSet().ListItems());
    }
    //For user story 6,7,8-related to checkout
    public void checkOutBook()
    {
        Scanner inputScanner=new Scanner(System.in);
        System.out.println("Enter the ISBN to check out");
        String ISBN=inputScanner.next();
        String resp=Library.getBookReservationManager().checkOut(ISBN);
        System.out.println(resp);
    }
    //For user story 9,10,11-related to return of books
    public void returnBook()
    {
        Scanner inputScanner=new Scanner(System.in);
        System.out.println("Enter the ISBN of the book to be returned");
        String isbn=inputScanner.next();
        String return_response=Library.getBookReservationManager().returnbook(isbn);
        System.out.println(return_response);
    }
    public void displayListofMovies()
    {
        System.out.println("MovieID\t\tTitle\t\tDate\t\tDirector\t\tRating\n");
        System.out.println(Library.getMovieReservationManager().getMoviesRepository().getMovieSet().ListItems());

    }
    public void checkoutMovies()
    {
        String movieId;
        Scanner inputScanner=new Scanner(System.in);
        System.out.println("Enter the MovieID to check out");
        movieId= String.valueOf(inputScanner.next());
        String resp=Library.getMovieReservationManager().checkOut(movieId);
        System.out.println(resp);
    }
    public void returnmovie()
    {
        String movieId;
        Scanner inputScanner=new Scanner(System.in);
        System.out.println("Enter the MovieID to return");
        movieId= String.valueOf(inputScanner.next());
        String resp=Library.getMovieReservationManager().returnmovie(movieId);
        System.out.println(resp);
    }
    public void displayProfile()
    {
        System.out.println(user);
    }
    public void processBookRelatedOperations() {
        char choice;

        do {
            System.out.println("Choose 1 to list available books");
            System.out.println("Choose 2 to checkout available books");
            System.out.println("Choose 3 to return checkedout books");
            System.out.println("Choose q to return to previous menu");
            choice = acceptCharacter();
            switch (choice) {
                case '1':
                    displayListofBooks();break;

                case '2':
                    checkOutBook();
                    break;
                case '3':
                    returnBook();
                    break;
                default:
                    if(choice != 'q')
                    System.out.println("Choose a valid option");
            }
        } while (choice != 'q');
    }
    public void processMovieRelatedOperations()
    {
        char choice;

        do {
            System.out.println("Choose 1 to list available movies");
            System.out.println("Choose 2 to checkout available movies");
            System.out.println("Choose 3 to return checkedout movies");
            System.out.println("Choose q to return to previous menu");
            choice = acceptCharacter();
            switch (choice) {
                case '1':
                    displayListofMovies();break;
                case '2':
                    checkoutMovies();
                    break;
                case '3':
                    returnmovie();
                    break;
                default:
                    if(choice != 'q')
                    System.out.println("Choose a valid option");
            }
        } while (choice != 'q');
    }


    //for stories 3,4,5
    public void processUserInput(char choice)
    {
        switch (choice) {
            case '1':
                displayProfile();
                break;

            case '2':
                processBookRelatedOperations();
                break;
            case '3':
                processMovieRelatedOperations();
                break;
            case '4': {
                UserLoginController.logout(user.getUserCredential().getLibraryNumber(), user.getUserCredential().getPassword());
                BibilioTechaUI ui=new BibilioTechaUI();
                ui.run();
                break;
            }
            default:
                if(choice != 'q')
                    System.out.println("Select a valid option!");break;
        }
    }//
    public char acceptCharacter()
    {
        Scanner inputScanner=new Scanner(System.in);
        char choice;
        choice = inputScanner.next().charAt(0);
        return choice;
    }
    public void displayMenu()
    {
        char choice;
        while(true)
        {
            System.out.println("Please enter a choice");
            System.out.println("Choose 1 to View Profile details");
            System.out.println("Choose 2 to check list of books/checkout and return of books");
            System.out.println("Choose 3 to check list of movies/checkout and return of movies");
            System.out.println("Choose q to exit");
            System.out.println("Enter your choice :");
           choice=acceptCharacter();
            processUserInput(choice);
            if(choice=='q')
                System.exit(0);
        }
    }

}
